<?php

namespace Yolo\Service\Apis;

use Hyperf\Context\ApplicationContext;
use Yolo\Service\Annotations\RPC;
use Yolo\Service\Endpoint;
use Yolo\Service\Enums\RPCType;

class ServiceApi
{
    #[RPC([
        'validation' => [],
        'description' => 'Ping接口，此接口用于检查服务是否宕机',
    ])]
    public function ping(): string
    {
        return 'pong';
    }

    #[RPC([
        'validation' => [],
        'description' => '获取所有endpoint',
    ])]
    public function getEndpointList(array $args)
    {
        $endpoints = ApplicationContext::getContainer()->get(Endpoint::class);
        $type = $args['type'] ?? '';

        if ($type === RPCType::RPC->value) {
            return $endpoints->getInternalEndPoints();
        } elseif ($type === RPCType::TUNNEL->value) {
            return $endpoints->getTunnelEndPoints();
        } elseif ($type === RPCType::RESTFUL->value) {
            return $endpoints->getRestfulEndPoints();
        } else {
            return $endpoints->getEndPoints();
        }
    }

    #[RPC([
        'validation' => [],
        'description' => '获取服务构建信息',
    ])]
    public function getBuildInfo(): array
    {
        return [
            'os' => [
                'system' => php_uname(),
                'timestamp' => time(),
            ],
            'php' => [
                'version' => PHP_VERSION,
                'timezone' => date_default_timezone_get(),
                'sapi' => PHP_SAPI,
            ],
            'swoole' => [
                'version' => SWOOLE_VERSION,
            ]
        ];
    }
}
